package pta;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author tmh
 * @date 2024/12/18 16:51
 * @description
 */
public class T6_5编写Student类使用泛型和集合框架编写StudentList类_实现计算班级平均分的功能 {
    public static void main(String[] args) {
        StudentList sl = new StudentList();
        List<Student> list = sl.constructStudentList();
        System.out.println(sl.computeAverageScore(list));
    }
}

class Student {
    private String name;
    private String classCode;
    private int score;

    public String getName() {
        return name;
    }

    public Student(String name, String classCode, int score) {
        this.name = name;
        this.classCode = classCode;
        this.score = score;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getClassCode() {
        return classCode;
    }

    public void setClassCode(String classCode) {
        this.classCode = classCode;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}

class StudentList {

    Scanner scanner;

    public Student readInStudent() {
        String name = scanner.next();
        if (name.equals("#")) {
            return null;
        }
        String classCode = scanner.next();
        int score = scanner.nextInt();
        Student student = new Student(name, classCode, score);
        return student;
    }

    List<Student> constructStudentList() {
         scanner = new Scanner(System.in);
        List<Student> list = new ArrayList<>();
        String code = scanner.next();
        Student stu = readInStudent();
        while (stu != null) {
            if (stu.getClassCode().equals(code)){
                list.add(stu);
            }

            stu = readInStudent();
        }
        scanner.close();
        return list;
    }

    double computeAverageScore(List<Student> list) {
        int sum = 0;
        for (Student student : list) {
            sum+=student.getScore();
        }
        return sum / list.size();
    }

}
